package cn.bone.rss.dao;

import cn.bone.rss.bean.NewsDo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by superbone on 2017/3/13.
 */
@Repository
public class RssDao {

    @Resource
    private JdbcTemplate template;

    public List<NewsDo> list(int count){
        StringBuffer sql = new StringBuffer();
        sql.append(" select news_id,news_title,news_source,news_content,news_lastupdatetime ");
        sql.append(" from news t ");
        sql.append(" where t.news_type = 1 and t.news_approvalstatus = 3 ");
        sql.append(" order by t.news_lastupdatetime desc limit 0,? ");
        return template.query(sql.toString(), new RowMapper<NewsDo>(){

            public NewsDo mapRow(ResultSet rs, int arg1) throws SQLException {
                NewsDo news = new NewsDo();
                int newsId = rs.getInt("news_id");
                news.setId(newsId);
                news.setTitle(rs.getString("news_title"));
                news.setSource(rs.getString("news_source"));
                news.setDescription(rs.getString("news_content"));
                news.setLink("http://dcwbapp.hsdcw.com/dcf/index.php/Home/Index/info/isfront/-1/infoId/"+newsId+"/tableType/1.html");
                news.setPubDate(new Date(rs.getTimestamp("news_lastupdatetime").getTime()));
                return news;
            }}, count);
    }
}
